package com.manage;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.manage.base.BaseActivity;
import com.manage.sharedpreferences.SPConfig;
import com.manage.util.RequestUtils;
import com.manage.util.ToastUtils;

/**
 * 按日期查看统计
 * @author liyoro
 *
 */
public class AccountsBEActivity extends BaseActivity implements OnClickListener {
	private static String TAG = "AccountsBEActivity";

	private TextView tvTimeBegin = null;
	private TextView tvTimeEnd = null;
	private Button btnConfirm = null;
	
	private TextView tvOrderCounts = null;
	private TextView tvAccepted = null;
	private TextView tvDelivery = null;
	private TextView tvAccounted = null;
	private TextView tvCancel = null;
	private TextView tvTurnover = null;
	private Button btnOrderDetails = null;

	private LinearLayout view1 = null;
	private LinearLayout view2 = null;
	private LinearLayout view3 = null;
	private LinearLayout view4 = null;
	private LinearLayout view5 = null;
	private LinearLayout view6 = null;
	private LinearLayout view7 = null;
	
	
	private void show() {
		view1.setVisibility(View.VISIBLE);
		view2.setVisibility(View.VISIBLE);
		view3.setVisibility(View.VISIBLE);
		view4.setVisibility(View.VISIBLE);
		view5.setVisibility(View.VISIBLE);
		view6.setVisibility(View.VISIBLE);
		view7.setVisibility(View.VISIBLE);
	}
	
	private void hide() {
		view1.setVisibility(View.GONE);
		view2.setVisibility(View.GONE);
		view3.setVisibility(View.GONE);
		view4.setVisibility(View.GONE);
		view5.setVisibility(View.GONE);
		view6.setVisibility(View.GONE);
		view7.setVisibility(View.GONE);
	}

    private final int DATE_DIALOG_BEGIN = 1;
    private final int DATE_DIALOG_END = 2; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_manage_accounts_be);

		hide();
	}
	
	@Override
	public String getLeftBtnName() {
		return "返回";
	}

	@Override
	public String getTitleName() {
		return "按日期统计";
	}

	@Override
	public boolean showButton() {
		return true;
	}
	
	@Override
	public void initView() {
		tvTimeBegin = (TextView)findViewById(R.id.tv_time_begin);
		tvTimeBegin.setOnClickListener(this);
		tvTimeEnd = (TextView)findViewById(R.id.tv_time_end);
		tvTimeEnd.setOnClickListener(this);
		btnConfirm = (Button)findViewById(R.id.btn_confirm);
		btnConfirm.setOnClickListener(this);
		
		tvOrderCounts = (TextView)findViewById(R.id.tv_received);
		tvAccepted = (TextView)findViewById(R.id.tv_accepted);
		tvDelivery = (TextView)findViewById(R.id.tv_delivery);
		tvAccounted = (TextView)findViewById(R.id.tv_accounted);
		tvCancel = (TextView)findViewById(R.id.tv_cancel);
		tvTurnover = (TextView)findViewById(R.id.tv_turnover);
		btnOrderDetails =(Button)findViewById(R.id.btn_check_order);
		btnOrderDetails.setOnClickListener(this);
		
		view1 = (LinearLayout)findViewById(R.id.layout1);
		view2 = (LinearLayout)findViewById(R.id.layout2);
		view3 = (LinearLayout)findViewById(R.id.layout3);
		view4 = (LinearLayout)findViewById(R.id.layout4);
		view5 = (LinearLayout)findViewById(R.id.layout5);
		view6 = (LinearLayout)findViewById(R.id.layout6);
		view7 = (LinearLayout)findViewById(R.id.layout7);
		
	}
	
	protected Dialog onCreateDialog(int id) {  
        //用来获取日期和时间的  
        Calendar calendar = Calendar.getInstance();   
          
        Dialog dialog = null;  
        switch(id) {  
            case DATE_DIALOG_BEGIN:  
                DatePickerDialog.OnDateSetListener dateListenerbegin =   
                    new DatePickerDialog.OnDateSetListener() {  
                        @Override  
                        public void onDateSet(DatePicker datePicker,   
                                int year, int month, int dayOfMonth) { 
                             //Calendar月份是从0开始,所以month要加1  
                            tvTimeBegin.setText(year + "年" +   
                                    (month+1) + "月" + dayOfMonth + "日");  
                        }  
                    };  
                dialog = new DatePickerDialog(this,  
                		dateListenerbegin,  
                        calendar.get(Calendar.YEAR),  
                        calendar.get(Calendar.MONTH),  
                        calendar.get(Calendar.DAY_OF_MONTH));  
                break;  
            case DATE_DIALOG_END:  
            	DatePickerDialog.OnDateSetListener dateListenerend =   
                	new DatePickerDialog.OnDateSetListener() {  
                    	@Override  
                    	public void onDateSet(DatePicker datePicker,   
                            int year, int month, int dayOfMonth) { 
                    			//Calendar月份是从0开始,所以month要加1  
                    			tvTimeEnd.setText(year + "年" +   
                                (month+1) + "月" + dayOfMonth + "日");  
                    	}  
                };  
                dialog = new DatePickerDialog(this,  
                		dateListenerend,  
                		calendar.get(Calendar.YEAR),  
                		calendar.get(Calendar.MONTH),  
                		calendar.get(Calendar.DAY_OF_MONTH));  
                break;  
            default:  
                break;  
        }  
        return dialog;  
    }  
	
	public Handler handler = new Handler() {
		public void handleMessage(Message msg) { 
			switch(msg.what) {
			case SUCCESS:
				Bundle bundle = msg.getData();
				String res = bundle.getString("res");
				
				try {
					show();
					JSONObject temp = new JSONObject(res.substring(1, res.length() -1));
					Log.i(TAG, temp.toString());
					JSONObject temp2 = new JSONObject(temp.getString("return_info"));
					
					tvOrderCounts.setText(temp2.getString("total_orders"));
					tvAccepted.setText(temp2.getString("total_accepted_orders"));
					tvDelivery.setText(temp2.getString("total_delivery_orders"));
					tvAccounted.setText(temp2.getString("total_accounted_orders"));
					tvCancel.setText(temp2.getString("total_cancel_orders"));
					tvTurnover.setText(temp2.getString("turnover"));
					
				} 
				catch (JSONException e) {
					e.printStackTrace();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case FAIL: //
				ToastUtils.show(AccountsBEActivity.this, "获取数据失败，请稍侯重试！", true);
				break;
			}
		}
	};
	
	@Override
	public void initDatas() {

//		String time;
	    //设置时间格式
//		SimpleDateFormat format=new SimpleDateFormat( "yyyy年MM月dd日");
//		time = format.format((new Date()));
		
		Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
		cal.add(Calendar.DAY_OF_MONTH, -30);
		
		String token = sp.getString(SPConfig.ACCESSTOKEN, "0");
		String shopID = sp.getString(SPConfig.SHOPID, "");
		String beginTime = tvTimeBegin.getText().toString();
		String endTime = tvTimeEnd.getText().toString();

		Log.i(TAG, "aaaaaaaa:"+beginTime+";"+endTime);
		
		if(beginTime.indexOf("年") != -1 && endTime.indexOf("年") != -1) {
			RequestUtils.getData(token, shopID, beginTime, endTime, ACCOUNTBEURL, handler, this.requestList);
		} else {
			ToastUtils.show(AccountsBEActivity.this, "请选择日期！", true);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_check_order:
			Intent mIntent = new Intent();
			mIntent.setClass(AccountsBEActivity.this, AccountsOrderDetailActivity.class);
			
			startActivity(mIntent);
			break;
		case R.id.tv_time_begin:
			showDialog(DATE_DIALOG_BEGIN);  
			break;
		case R.id.tv_time_end:
			showDialog(DATE_DIALOG_END);  
			break;
		case R.id.btn_confirm:
			initDatas();
			break;
		}
		
	}
}
